<template>
  <div>
    <page-list :x-uri="uri" :x-tabs="tabs" :doactions="doactions" :custom-components="custom">
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'content.agreement.create'" size="mini" type="primary" @click="create">新建</el-button>
      </template>
      <!-- 多选操作 -->
      <template v-slot:table-button="scope">
        <el-button v-auth="'content.sensitive.edit'" size="mini" @click="disable(scope)">禁用</el-button>
        <el-button v-auth="'content.sensitive.edit'" size="mini" @click="enable(scope)">启用</el-button>
        <el-button v-auth="'content.sensitive.delete'" size="mini" @click="deletes(scope)">删除</el-button>
      </template>
    </page-list>
  </div>
</template>

<script>
import PageList from '@public/template/page_list.vue';
import Status from './components/status.vue';

export default {
  components: {
    PageList
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/admin/content/sensitive/list',
      tabs: [
        {
          title: '敏感词管理',
          uri: '/content/sensitive/list',
          meta: {
            permission: 'content.sensitive.list'
          }
        }
      ],
      doactions: [
        {
          title: '编辑',
          meta: { permission: 'content.sensitive.edit' },
          callback: this.edit
        },
        {
          title: '删除',
          meta: { permission: 'content.sensitive.delete' },
          callback: this.delete
        }
      ],
      custom: {
        status: Status
      }
    };
  },
  methods: {
    create: function() {
      this.$router.push({ path: '/content/sensitive/create' });
    },
    edit: function({ row }) {
      this.$router.push({ path: '/content/sensitive/' + row.id + '/edit' });
    },
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/admin/content/sensitive/' + row.id,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量禁用
    disable: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要禁用的敏感词');
        return false;
      }
      this.$box
          .confirm('确认禁用吗')
          .then(() => {
            this.$http.put(
                '/admin/content/sensitive/disables',
                {
                  ids: ids
                },
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    },
    // 批量上架
    enable: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要启用的敏感词');
        return false;
      }
      this.$box
          .confirm('确认启用吗')
          .then(() => {
            this.$http.put(
                '/admin/content/sensitive/enables',
                {
                  ids: ids
                },
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    },
    // 批量删除
    deletes: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要删除的敏感词');
        return false;
      }
      ids = ids.join(',');
      this.$box
          .confirm('确认删除吗')
          .then(() => {
            this.$http.delete(
                '/mall/admin/product/deletes/' + ids,
                {},
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    }
  }
};
</script>
